package cn.icanci.loopstack.ddk.web.controller.config;

import cn.icanci.loopstack.ddk.admin.biz.service.AppService;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.TextValue;
import cn.icanci.loopstack.ddk.common.model.config.AppVO;
import cn.icanci.loopstack.ddk.common.result.R;
import cn.icanci.loopstack.ddk.web.form.AppQueryForm;
import cn.icanci.loopstack.ddk.web.mapping.AppWebMapping;
import cn.icanci.loopstack.ddk.web.model.App;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/06 21:11
 */
@RestController
@RequestMapping("/ddk/app")
public class AppController {
    @Resource
    private AppService    appService;

    @Resource
    private AppWebMapping appWebMapping;

    @PostMapping("query")
    public R query(@RequestBody AppQueryForm form) {
        PageList<AppVO> queryPage = appService.queryPage(appWebMapping.web2vo(form.getApp()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize());
        PageList<App> ret = new PageList<>(appWebMapping.vos2webs(queryPage.getData()), queryPage.getPaginator());
        return R.builderOk().data("queryPage", ret).build();
    }

    @PostMapping("save")
    public R save(@RequestBody App app) {
        appService.save(appWebMapping.web2vo(app));
        return R.builderOk().build();
    }

    @GetMapping("validateAppId/{appId:.*}")
    public R validateAppId(@PathVariable("appId") String appId) {
        AppVO app = appService.queryByAppId(appId);
        return R.builderOk().data("result", app == null).build();
    }

    @GetMapping("validateAppName/{appName:.*}")
    public R validateAppName(@PathVariable("appName") String appName) {
        AppVO app = appService.queryByAppName(appName);
        return R.builderOk().data("result", app == null).build();
    }

    @GetMapping("loadSelector")
    public R loadSelector() {
        List<TextValue> textValues = appService.loadSelector();
        return R.builderOk().data("textValues", textValues).build();
    }
    @GetMapping("loadAppIdSelector")
    public R loadAppIdSelector() {
        List<TextValue> textValues = appService.loadAppIdSelector();
        return R.builderOk().data("textValues", textValues).build();
    }
}
